package com.play.ballen.home.ui

import android.content.Context
import android.content.Intent
import android.os.Build
import android.view.ViewGroup
import android.view.ViewParent
import android.webkit.WebSettings
import com.play.ballen.R
import com.play.ballen.databinding.ActivityNoticeDetailBinding
import com.play.ballen.home.entity.ArticleInfo
import com.play.ballen.home.vm.ArticleVM
import com.yuven.appframework.base.BaseStdTitleActivity
import com.yuven.baselib.utils.ResourceUtil

class NoticeDetailActivity : BaseStdTitleActivity<ArticleVM, ActivityNoticeDetailBinding>() {

    var mInfo: ArticleInfo? = null

    companion object {
        @JvmStatic
        fun start(context: Context, info: ArticleInfo) {
            val starter = Intent(context, NoticeDetailActivity::class.java)
            starter.putExtra("info", info)
            context.startActivity(starter)
        }
    }

    override fun viewModelClass(): Class<ArticleVM> {
        return ArticleVM::class.java
    }

    override fun contentLayoutId(): Int {
        return R.layout.activity_notice_detail
    }

    override fun getToolBarTitle(): String {
        return ResourceUtil.getString(R.string.notice_detail_title)
    }

    override fun initView() {
        super.initView()

        dataBinding.webView.apply {
            isDrawingCacheEnabled = true
            requestFocus() //此句可使html表单可以接收键盘输入
            isFocusable = true
            val mWebSettings = settings
            // 网页内容的宽度是否可大于WebView控件的宽度
            mWebSettings.loadWithOverviewMode = false
            //隐藏原生的缩放控件
            mWebSettings.displayZoomControls = false
            mWebSettings.useWideViewPort = true
            mWebSettings.setGeolocationEnabled(true)
            mWebSettings.domStorageEnabled = true
            mWebSettings.javaScriptEnabled = true
            mWebSettings.databaseEnabled = true
            // 是否应该支持使用其屏幕缩放控件和手势缩放
            mWebSettings.setSupportZoom(true)
            mWebSettings.builtInZoomControls = true
            mWebSettings.loadsImagesAutomatically = true //支持自动加载图片
            mWebSettings.defaultTextEncodingName = "utf-8" //设置编码格式
            setBackgroundColor(0)
            setInitialScale(100)

            /**Android 5.0后默认不支持混合模式，即网页为https，图片为http  */
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mWebSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            }
        }

        mInfo = intent.getSerializableExtra("info") as? ArticleInfo
        setDetailInfo(mInfo)
    }

    override fun initObserver() {
        super.initObserver()

        /*viewModel.articleInfo.observe(this) {
            setDetailInfo(it)
        }*/
    }

    private fun setDetailInfo(info: ArticleInfo?) {
        if (info == null) {
            return
        }
        dataBinding.tvName.text = info.title
        dataBinding.tvTime.text = info.add_time

        if (info.link.isNotEmpty()) {
            dataBinding.webView.loadUrl(info.link)
        }

        /*info.content?.let {
            dataBinding.webView.loadDataWithBaseURL("about:blank", it, "text/html", "utf-8", null)
        }*/
    }

    override fun initData() {
        /*mInfo?.id?.let {
            viewModel.getArticleDetail(it)
        }*/
    }

    override fun onDestroy() {
        super.onDestroy()
        dataBinding.webView.apply {
            val parent: ViewParent? = parent
            if (parent != null) {
                (parent as ViewGroup).removeView(this)
            }
            this.handler?.removeCallbacksAndMessages(null)
            clearHistory()
            clearCache(true)
            // Loading a blank page is optional, but will ensure that the WebView isn't doing anything when you destroy it.
            loadUrl("about:blank")
            removeAllViews()
            destroyDrawingCache()
            destroy()
        }
    }

}